package org.jeecg.modules.crawler.job;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.crawler.entity.NewCrawlerInfo;
import org.jeecg.modules.crawler.service.INewCrawlerInfoService;
import org.jeecg.modules.crawler.service.INewCrawlerKeywordService;
import org.jeecg.modules.douyin.entity.SeuDy;
import org.jeecg.modules.douyin.service.ISeuDyService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务定时发送
 */
@Slf4j
public class SeuDYCrawlerJob implements Job {

	@Autowired
	private INewCrawlerInfoService newCrawlerInfoService;

	@Autowired
	private ISeuDyService dyService;


	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		WebClient webClient = getClient();
		webClient.getOptions().setJavaScriptEnabled(false);
		String result = null;
		List<NewCrawlerInfo> crawlerInfos = new ArrayList<>();

		try {
			result = getHtml("https://news.seu.edu.cn/5485/list.htm", webClient);
//			System.out.println(result);
			Document doc = Jsoup.parse(result);
			Elements elementsByClass = doc.getElementsByClass("con news_tu");
			for (Element byClass : elementsByClass) {
				for(int i =1; i <= 4 ; i++){
					try {
						Elements elements1 = byClass.getElementsByClass("news n"+i);
						String img = elements1.get(0).getElementsByClass("news_imgs").get(0).getElementsByTag("img").get(0).attr("src");
						String title = elements1.get(0).getElementsByClass("news_title").get(0).text();
						SeuDy seuDy = new SeuDy();
						seuDy.setTitle(title);
						seuDy.setImg("https://news.seu.edu.cn/"+img);
						dyService.saveOrUpdate(seuDy);
					}catch (Exception e){
						e.printStackTrace();
					}
				}

			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}


		try {
			result = getHtml("https://www.seu.edu.cn/", webClient);
//			System.out.println(result);
			Document doc = Jsoup.parse(result);
			Elements elementsByClass = doc.getElementsByClass("dou");
			for (Element byClass : elementsByClass) {
				Elements elements1 = byClass.getElementsByClass("item");
				for (Element itemClass : elements1) {
					String title = itemClass.getElementsByClass("title").get(0).text();
					String html =  itemClass.getElementsByClass("lazy").get(0).attr("style");
					String url = "https://www.seu.edu.cn"+(html.split("\\(")[1]).split("\\)")[0];
					SeuDy seuDy = new SeuDy();
					seuDy.setTitle(title);
					seuDy.setImg(url);
					dyService.saveOrUpdate(seuDy);
				}

			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}


	}
//
//	public static void main(String[] args) {
//		WebClient webClient = getClient();
//		webClient.getOptions().setJavaScriptEnabled(false);
//		String result = null;
//		List<NewCrawlerInfo> crawlerInfos = new ArrayList<>();
//
//		try {
//			result = getHtml("https://www.seu.edu.cn/", webClient);
////			System.out.println(result);
//			Document doc = Jsoup.parse(result);
//			Elements elementsByClass = doc.getElementsByClass("dou");
//			for (Element byClass : elementsByClass) {
//						Elements elements1 = byClass.getElementsByClass("item");
//						for (Element itemClass : elements1) {
//							String title = itemClass.getElementsByClass("title").get(0).text();
//							String html =  itemClass.getElementsByClass("lazy").get(0).attr("style");
//							String url = "https://www.seu.edu.cn"+(html.split("\\(")[1]).split("\\)")[0];
//							System.out.println(title);
//							System.out.println(url);
//						}
//
//			}
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//	}



	public  static WebClient  getClient(){
		WebClient  webClient = new WebClient(BrowserVersion.CHROME);

		webClient.getOptions().setThrowExceptionOnScriptError(false);
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
		webClient.getOptions().setActiveXNative(false);
		webClient.getOptions().setCssEnabled(false);
		webClient.getOptions().setJavaScriptEnabled(true);
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());
		webClient.getOptions().setTimeout(30000);
		webClient.setJavaScriptTimeout(30000);
		webClient.getOptions().setRedirectEnabled(true);
		return  webClient;

	}


	public  static String  getHtml(String url, WebClient webClient) throws InterruptedException {
		HtmlPage page = null;
		try {
			webClient.getOptions().setUseInsecureSSL(true);
			page = webClient.getPage(url);
		} catch (Exception e) {
			e.printStackTrace();
			webClient.close();

		}

		//该方法阻塞线程
		webClient.waitForBackgroundJavaScript(30000);
		return   page.asXml();
	}

}
